package com.hq.fxh.No3_TuiGuang.MyMoBan.Activity.item;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.AppCompatCheckBox;
import android.text.Editable;
import android.text.Html;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.hq.fxh.CustomerView.PopupWindow.PopupForModeShowTime;
import com.hq.fxh.CustomerView.switchbutton.SwitchButton;
import com.hq.fxh.No3_TuiGuang.MyMoBan.Activity.AdBannerInfo;
import com.hq.fxh.R;
import com.hq.fxh.Utils.GlideUtils;
import com.hq.fxh.Utils.SharedPreferenceUtils;


public class BigAdItem extends LinearLayout {
    /**
     * 删除按钮
     */
    private ImageButton ibClose;
    /**
     * 图片添加
     */
    private FrameLayout flBigPicAdd;
    /**
     * 广告类型名称
     */
    private TextView tvAdTypeName;
    /**
     * 通栏广告图片
     */
    private ImageView ivAd;
    /**
     * 链接
     */
    private EditText etName;
    private SwitchButton sbSwitch;
    private TextView tvSwitchInfo;
    private AppCompatCheckBox cbUseSpace;
    boolean flag = false;
    private AdBannerInfo adBannerInfo;
    private TextView tolink;
    private Spinner spinner_clickWay;
    private Spinner spinner_time;
    private boolean isFirst;
    private LinearLayout ll_ad_time;
    private TextView tv_des;
    private TextView showType;
    Context context;
    private PopupForModeShowTime popShowType;

    public BigAdItem(Context context) {
        super(context);
        this.context=context;
        inflate(context, R.layout.item_big_ad, this);
        findView();
        setSpinnerAdapter();
        tvAdTypeName.setText("大图通栏");
        ibClose.setVisibility(VISIBLE);
        ll_ad_time.setVisibility(GONE);
        String des = "<font color=\"#d81b24\">" + "*  大图广告设置为展现时，其他广告类型将不展示。</font>";
        tv_des.setText(Html.fromHtml(des));
    }


    public void findView() {

        spinner_clickWay = (Spinner) findViewById(R.id.spinner_clickWay);
        spinner_time = (Spinner) findViewById(R.id.spinner_time);
        flBigPicAdd = (FrameLayout) findViewById(R.id.flBigPicAdd);
        ibClose = (ImageButton) findViewById(R.id.ibClose);
        tvAdTypeName = (TextView) findViewById(R.id.tvAdTypeName);
        ivAd = (ImageView) findViewById(R.id.ivAd);
        etName = (EditText) findViewById(R.id.etName);
        sbSwitch = (SwitchButton) findViewById(R.id.sbSwitch);
        tvSwitchInfo = (TextView) findViewById(R.id.tvSwitchInfo);
        showType = (TextView) findViewById(R.id.tv_BigAD_ShowType);

        tolink = (TextView) findViewById(R.id.tolink);
        cbUseSpace = (AppCompatCheckBox) findViewById(R.id.cbUseSpace);
        ll_ad_time = (LinearLayout) findViewById(R.id.ll_ad_time);
        tv_des = (TextView) findViewById(R.id.tv_des);
      initListen();
    }

    private void initListen() {

        String[] type=  getResources().getStringArray(R.array.clickway);
        popShowType = new PopupForModeShowTime((Activity) context,type, new PopupForModeShowTime.ShareModleCallBack() {
            @Override
            public void callSure(int position,String comment) {
                showType.setText(comment);
                popShowType.dismiss();
                setShowType(adBannerInfo,position);

            }
        });
        showType.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                popShowType.show(view);
            }
        });
    }

    private void setShowType(AdBannerInfo adBannerInfo, int position) {
        if (!isFirst) {
            etName.setText(null);
            cbUseSpace.setChecked(false);
        }
        isFirst = false;
        AdBannerInfo.AdvertDetailBean adBean = adBannerInfo.getAdvert_detail();
        adBean.setAction(position + 1);
        cbUseSpace.setVisibility(GONE);
        if (position == 0) {//跳转链接
            cbUseSpace.setVisibility(VISIBLE);
            tolink.setText("跳转链接");
            changeDrawable(R.mipmap.admodle_link);
            etName.setInputType(InputType.TYPE_CLASS_TEXT);
            etName.setHint("请输入您的链接地址");
        } else if (position == 1) {//拨打电话
            tolink.setText("拨打电话");
            changeDrawable(R.mipmap.admodle_dianhua);
            etName.setInputType(InputType.TYPE_CLASS_PHONE);
            etName.setHint("请输入您的联系电话");
        } else if (position == 2) {
            tolink.setText("联系QQ");
            changeDrawable(R.mipmap.admodle_qq);
            etName.setHint("请输入您的QQ号码");
            etName.setInputType(InputType.TYPE_CLASS_NUMBER);
        }
    }

    public void setSpinnerAdapter() {
        ArrayAdapter<String> cadapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.clickway));
        cadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner_clickWay.setAdapter(cadapter);

        ArrayAdapter<String> timeAdapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.time));
        timeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner_time.setAdapter(timeAdapter);

    }

    public void setInfo(final AdBannerInfo info) {//网络请求回来后（从服务器获取到的已保存模板）
        isFirst = true;
        adBannerInfo = info;
        AdBannerInfo.AdvertDetailBean Advert_detail = info.getAdvert_detail();

        if (Advert_detail != null) {
            //    ImageLoader.getInstance().load(getContext(), ivAd, Advert_detail.getPic());
            String pic = Advert_detail.getPic();
            if (pic == null || pic.equals("")) {

            } else {
                GlideUtils.loadImage(getContext(), pic, ivAd);

            }
            spinner_clickWay.setSelection((Advert_detail.getAction() != null ? Advert_detail.getAction() : 1) - 1);
            etName.setText(Advert_detail.getHref());
            if (spinner_clickWay.getSelectedItemPosition() == 0) {
                cbUseSpace.setChecked(true);
            }
        } else {
            Advert_detail = new AdBannerInfo.AdvertDetailBean();
            info.setAdvert_detail(Advert_detail);
        }

        sbSwitch.setVisibility(View.VISIBLE);
        sbSwitch.setChecked(BooleanUtils.intToBool(info.getIs_carousel()));

        sbSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                info.setIs_carousel(BooleanUtils.BoolToInt(isChecked));
            }
        });
        cbUseSpace.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if (!compoundButton.isPressed()) return;    //加这一条，否则当我setChecked()时会触发此listener
                flag = !flag;
                cbUseSpace.setChecked(flag);
              /*     final User user = null;
             if (user != null) {
                    if (!user.getIsHaveHomePage()) {

                        ToastUtils.show(getContext(), "您暂未开通形象空间，请前往 我的-形象空间设置。");
                    }
                }*/
                if (isChecked) {
                    int uid = SharedPreferenceUtils.getMemberId(getContext());
//                    String url = "http://com.ledianduo.com/xxkj/index.html?MerchantId=" + uid;
//                    String url= CommonUtils.getLddConfig().getCdnSite()+"/xxkj/index.html?MerchantId="+ uid;
                    String url = SharedPreferenceUtils.getSpaceImageUrl(getContext());
                    info.getAdvert_detail().setHref(url);
                    etName.setText(url);
                } else {
                    info.getAdvert_detail().setHref("");
                    etName.setText("");
                }
            }
        });
        spinner_time.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                AdBannerInfo.AdvertDetailBean adBean = info.getAdvert_detail();
                adBean.setDuring(position + 3);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        spinner_clickWay.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        etName.setFilters(new InputFilter[]{new EmojiFilter()});
        etName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                String str = s.toString();
                AdBannerInfo.AdvertDetailBean advertDetailBean = info.getAdvert_detail();
                if (advertDetailBean == null) {
                    advertDetailBean = new AdBannerInfo.AdvertDetailBean();
                }
                advertDetailBean.setHref(str.toString());
                info.setAdvert_detail(advertDetailBean);
            }
        });
    }

    private void changeDrawable(int drawableId) {
        Drawable drawable = getResources().getDrawable(drawableId);
        drawable.setBounds(6, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        tolink.setCompoundDrawables(drawable, null, null, null);
    }

    public void setText(boolean isCheck) {
        if (isCheck) {
            tvSwitchInfo.setText(R.string.ad_on_tip);

        } else {
            tvSwitchInfo.setText(R.string.ad_off_tip);

        }
    }

    public void setOnClickListener(final OnClickListener listener) {
        ibClose.setOnClickListener(listener);
        flBigPicAdd.setOnClickListener(listener);
    }

    public void setImage(String url) {
        //   ImageLoader.getInstance().load(getContext(), ivAd, url);
        GlideUtils.loadImage(getContext(), url, ivAd);
        if (adBannerInfo != null && adBannerInfo.getAdvert_detail() != null) {
            adBannerInfo.getAdvert_detail().setPic(url);
        } else {
            AdBannerInfo.AdvertDetailBean advertDetailBean = new AdBannerInfo.AdvertDetailBean();
            advertDetailBean.setPic(url);
            advertDetailBean.setHref("");
            adBannerInfo.setAdvert_detail(advertDetailBean);
        }
    }
}
